package com.lebang.controller.api.oceanengine;

import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Login;
import com.lebang.model.back.KsapiModel;
import com.lebang.service.mongodb.KsApiModelMongodb;
import com.lebangweb.common.TextUtil;
import com.lebangweb.encrypt.MessageDigestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 快手
 *
 * @Author: xwj
 * @Date: 2020/6/16 19:45
 * @Version 1.0
 */
@Slf4j
@Controller
@RequestMapping("/api/oceanengine/ks")
public class KsApi {
    @Autowired
    private KsApiModelMongodb ksApiModelMongodb;
    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 接口一 快手用户点击广告
     */
    @GetMapping("/click")
    @ResponseBody
    public String click(String type, String aid, String cid, String did, String dname, String imei, String mac,
                        String oaid, String androidid, Long TIMESTAMP,
                        String callback, String ip, String csite, HttpServletRequest request) {

        KsapiModel ksapiModel = new KsapiModel();
        ksapiModel.setAid(aid);
        ksapiModel.setCid(cid);
        ksapiModel.setDid(did);
        ksapiModel.setDname(dname);
        ksapiModel.setImei(imei);
        ksapiModel.setMac(mac);
        ksapiModel.setOaid(oaid);
        ksapiModel.setAndroidid(androidid);
        ksapiModel.setTIMESTAMP(TIMESTAMP);
        ksapiModel.setCallback(callback);
        ksapiModel.setIp(ip);
        ksapiModel.setCsite(csite);
        ksapiModel.setStatus(0);
        ksapiModel.setCreatTime(LocalDateTime.now());
        ksApiModelMongodb.save(ksapiModel);
        return "ok";
    }


    @Login
    @GetMapping("/list")
    public String list(Model model, Integer id) {
        model.addAttribute("id", id);
        return "/ksmodel/list";
    }

    @Login
    @PostMapping("/list")
    @ResponseBody
    public JSONObject listData(HttpServletRequest request, @RequestParam Integer page,
                               @RequestParam Integer limit, String imei,
                               @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date star,
                               @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date end) {
        page = (page - 1);
        Sort sort = Sort.by(Sort.Direction.DESC, "creatTime");
        Query query = new Query();
        Criteria criteria = new Criteria();
        //设置查询时间范围
        if (!TextUtil.isEmpty(star)) {
            criteria.and("creatTime").gte(star).lte(end);
        }

        if (!TextUtil.isEmpty(imei)) {
            try {
                criteria.and("imei").is(MessageDigestUtil.encryptMD5(imei.getBytes()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        query.addCriteria(criteria);
//查询总条数
        long count = mongoTemplate.count(query, KsapiModel.class);
        Pageable pageable = PageRequest.of(page, limit, sort);
        query.with(pageable);
//查询数据
        List<KsapiModel> mongoVehicleUploadLogs = mongoTemplate.find(query.with(pageable), KsapiModel.class);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("data", mongoVehicleUploadLogs);
        jsonObject.put("count", count);
        return jsonObject;
    }

}
